﻿using IntelligentDataCollection.Tool;
using Quartz;
using System.Runtime;

namespace IntelligentDataCollection.JobTask
{
    public class DataCleanService : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                ClearLog();
            }
            catch
            {

            }
        }

        public void ClearLog()
        {
            try
            {
                string logPath = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/Temp";
                string logPath2 = AppDomain.CurrentDomain.BaseDirectory + "wwwroot/Export";
                DeleteDir(logPath);
                DeleteDir(logPath2);
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }
        }

        public void DeleteDir(string logPath)
        {
            try
            {
                if (!Directory.Exists(logPath))
                {
                    return;
                }
                DirectoryInfo folder_base = new DirectoryInfo(logPath);

                foreach (DirectoryInfo subDir in folder_base.GetDirectories())
                {
                    DateTime fileCreateTime = subDir.CreationTime;
                    if (fileCreateTime < DateTime.Now.AddDays(-1))
                    {
                        subDir.Delete();
                    }
                }
            }
            catch (Exception e)
            {
                LogHelper.Log(e.ToString());
            }
        }
    }
}
